//
//  NetworkManager+Extention.swift
//  新浪微博
//
//  Created by yaozhen on 2017/12/6.
//  Copyright © 2017年 ZYZ. All rights reserved.
//

import Foundation
extension NetworkManager{
    func statuList(since_id: Int64 = 0 ,max_id:Int64 = 0,completion: @escaping (_ list:[[String:AnyObject]]?,_ isSucces:Bool)->()) {
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        let params = ["since_id" : "\(since_id)" as AnyObject ,"max_id" : "\(max_id>0 ? max_id - 1 :  0)"] as [String : AnyObject]
        NetworkManager.shared.tokenRequest(URLString: urlString, params: params) { (json, isSucces) in
            let result = json?["statuses"] as?[[String:AnyObject]]
            
            completion(result,isSucces)
            
        }

    }
    
    //返回新浪微博的未读数量
    func unreadCount(completion: @escaping (_ count: Int) ->()) {
        guard let uuid = userAcount.uid else {
            return
        }
        let  urlString = "https://rm.api.weibo.com/2/remind/unread_count.json"
        let params = ["url":uuid] as [String : AnyObject]
        tokenRequest(URLString: urlString, params: params) { (json, _) in
            let dict = json as? [String : AnyObject]
            let count1 = dict?["status"] as? Int
            
            completion(count1 ??  0)
        }
    }
}
extension NetworkManager{
    //加载AccessToken
    func loadAccessToken(code: String, completion: @escaping (_ isSucces:Bool) -> ()){
        let url = "https://api.weibo.com/oauth2/access_token"
        let params = ["client_id" : AppKey,
                      "client_secret" : AppSecret,
                      "grant_type" : "authorization_code",
                      "code" : code,
        "redirect_uri" : RedirectURl] as [String :AnyObject]
        request(method: .POST, URLString: url, params: params) { (json, isSucces) in
            self.userAcount.yy_modelSet(withJSON: json ?? [:])
            print(self.userAcount)
            //保存模型
            self.userAcount.saveAcount()
            //完成回调
            completion(isSucces)
        }
        
    }
}
